package com.example.juyixia;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class NewActiveActivity extends Activity implements OnClickListener{
	private Button mBt_Sure;
	private ImageView mIv_Back;
	private Myinfo myinfo;
	private ListView friends;
	JSONObject actievinfojson;
	public static final String bm="utf-8";
	StringBuffer setid;
	private EditText activename;
	Map<String, String> activeinfo=new HashMap<String, String>();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);  
        setContentView(R.layout.activity_newactive); 
        friends = (ListView)findViewById(R.id.selectfriend);
        activename=(EditText)findViewById(R.id.activename);
        //给链表添加数据
        List<Map<String, Object>> list=getData();
        //适配器，刚刚重写的！
        MyAdapterchecked myAdapter = new MyAdapterchecked(this, list);
        //设置适配器
        friends.setAdapter(myAdapter);
        initViews();
		
	}
	 public List<Map<String, Object>> getData() {
		    myinfo = (Myinfo)getApplication();
	        List<Myinfo> friendsinfo = myinfo.getUserfriend();
	        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
	        for (Myinfo info : friendsinfo) {
	        	Map<String, Object> map = new HashMap<String, Object>();
	        	map.put("image",  R.drawable.ic_launcher);
	        	map.put("userid", info.getUserid());
	        	map.put("username", info.getUsername());
	        	 list.add(map);
			}
	       
	        return list;
	    }
	private void initViews() {
		// TODO Auto-generated method stub
		mBt_Sure = (Button) findViewById(R.id.bt_sure);
		mIv_Back = (ImageView) findViewById(R.id.iv_back);
		
		mBt_Sure.setOnClickListener(this);
		mIv_Back.setOnClickListener(this);
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
		switch (v.getId()) {
		case R.id.bt_sure:
			activeinfo.clear();
			setid=new StringBuffer("");
			for(int i=0;i<friends.getChildCount();i++){
				
				LinearLayout myitem = (LinearLayout)friends.getChildAt(i);
				CheckBox chkone = (CheckBox) myitem.findViewById(R.id.friendchecked);
				if(chkone.isChecked()){
					TextView friendsid=(TextView) myitem.findViewById(R.id.usersfriendid);
					if(i<friends.getChildCount()-1){
					setid.append(friendsid.getText()).append(",");
					}else{
						setid.append(friendsid.getText());
					}
				}
				
			}
			activeinfo.put("发起人：",myinfo.getUsername());
			activeinfo.put("活动名：", activename.getText().toString());
			activeinfo.put("参与者：", setid.toString());
			//Toast.makeText(getApplicationContext(),activeinfo.toString(),Toast.LENGTH_SHORT).show();
			 actievinfojson=new JSONObject(activeinfo);
			new Thread() {
				public void run() {				
					Socket socket = null;
					try {
						// 创建Socket
						// socket = new Socket("192.168.1.110",54321);
						socket = new Socket("192.168.43.243", 54321); // IP一定是本地机器的ip
						
						// 向服务器发送消息
						PrintWriter out = new PrintWriter(
								new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(),bm)), true);
						out.println(actievinfojson.toString());
						out.close();
						// 关闭Socket
						socket.close();
					} catch (Exception e) {
						// TODO: handle exception
						
					}

					handler.sendEmptyMessage(0);// 执行完毕后给handler发送一个空消息
				}

				// 定义Handler对象
				private Handler handler = new Handler() {
					@Override
					// 当有消息发送出来的时候就执行Handler的这个方法
					public void handleMessage(Message msg) {
						super.handleMessage(msg);
						// 处理UI
						System.out.println("OK?");
					}
				};
			}.start();
			break;
		case R.id.iv_back:
			finish();
			break;
		default:
			break;
		}
		
	
	
	}

}
